//
//  SJBaseModelTool.m
//  Test
//
//  Created by zhaoshijie on 15/8/28.
//  Copyright (c) 2015年 zhaoshijie. All rights reserved.
//

#import "SJBaseModelTool.h"

@implementation SJBaseModelTool



/// 自定义初始化方法（带有自动映射属性的作用）
/// @param dic 数据字典
- (instancetype)initWithContentDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        
        
        /// 设置属性值
        [self setAttributeValueWithDic:dic];
        
        /// 判空处理
//        [self judgeNullHandle];
        /// 判空处理    全部，用于继承之后的基类
        [self judgeNullAllHandle];
        
        
    }
    return self;
}



/// 设置属性值
- (void)setAttributeValueWithDic:(NSDictionary *)dic
{
    //  获取所有的属性
    
    Class superClass = [self class];
       
   while (superClass) {
    

       u_int count = 0;
       objc_property_t *properties = class_copyPropertyList(superClass, &count);
       

       for (int i = 0; i < count; i++)
       {
           const char *propertyName = property_getName(properties[i]);
           NSString *attributeName = [NSString stringWithUTF8String:propertyName];
           
           SEL setMethod = [self getSetMethodWithAttName:attributeName];
           
           if ([self respondsToSelector:setMethod])
           {
               if(dic && ![dic isKindOfClass:[NSNull class]])
               {
                   //  取值
                   id value = dic[attributeName];
                   
                   [self performSelector:setMethod withObject:value];
               }
               
           }
       }
       
       free(properties);
       
       superClass = [superClass superclass];
       
   }
    
    
    
}



/// 判空处理
- (void)judgeNullHandle
{
    
    //  获取所有的属性
    
    u_int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    

    for (int i = 0; i < count; i++)
    {
        const char *propertyName = property_getName(properties[i]);
        NSString *attributeName = [NSString stringWithUTF8String:propertyName];
        
        
        SEL setMethod = [self getSetMethodWithAttName:attributeName];
        
        SEL getMethod = [self getMethodWithAttName:attributeName];
        
        if ([self respondsToSelector:getMethod])
        {
            
            id value ;
            
            id result = [self performSelector:getMethod withObject:nil];
            
            
            if(!result || [result isKindOfClass:[NSNull class]])
            {
                
                const char *propertyClass = property_getAttributes(properties[i]);
                NSString *attributeClass = [NSString stringWithUTF8String:propertyClass];
                
                
                if ([attributeClass rangeOfString:NSStringFromClass([NSString class])].location != NSNotFound)
                {
                    value = @"--";
                }
                
                if ([attributeClass rangeOfString:NSStringFromClass([NSNumber class])].location != NSNotFound)
                {
//                    value = @(0);
                }
                
                if ([attributeClass rangeOfString:NSStringFromClass([NSArray class])].location != NSNotFound)
                {
                    value = [[NSArray alloc]init];
                }
                
                if(value)
                {

                    if ([self respondsToSelector:setMethod])
                    {
                        [self performSelector:setMethod withObject:value];
                        
                    }
                    
                }
                
            }
            
        }
    }
    
    free(properties);
    
}



/// 判空处理    全部，用于继承之后的基类
- (void)judgeNullAllHandle
{
    
    Class superClass = [self class];
    
    
    while (superClass && ![NSStringFromClass(superClass) isEqualToString:NSStringFromClass([NSObject class])]) {
        
        //  获取所有的属性
        
        u_int count = 0;
        objc_property_t *properties = class_copyPropertyList(superClass, &count);
        

        for (int i = 0; i < count; i++)
        {
            const char *propertyName = property_getName(properties[i]);
            NSString *attributeName = [NSString stringWithUTF8String:propertyName];
            
            
            SEL setMethod = [self getSetMethodWithAttName:attributeName];
            
            SEL getMethod = [self getMethodWithAttName:attributeName];
            
            if ([self respondsToSelector:getMethod])
            {
                
                id value ;
                
                if([attributeName isEqualToString:@"stage"])
                {
                    //  过滤，release版本运行出现了一个未知的stage属性崩溃问题
                    
                    continue;
                }
                
                
                id result = [self performSelector:getMethod withObject:nil];
                
                
                if(!result || [result isKindOfClass:[NSNull class]])
                {
                    
                    const char *propertyClass = property_getAttributes(properties[i]);
                    NSString *attributeClass = [NSString stringWithUTF8String:propertyClass];
                    
                    
                    if ([attributeClass rangeOfString:NSStringFromClass([NSString class])].location != NSNotFound)
                    {
                        value = @"";
                    }
                    
                    if ([attributeClass rangeOfString:NSStringFromClass([NSNumber class])].location != NSNotFound)
                    {
                        value = @(0);
                    }
                    
                    if ([attributeClass rangeOfString:NSStringFromClass([NSArray class])].location != NSNotFound)
                    {
                        value = [[NSArray alloc]init];
                    }
                    
                    if(value)
                    {

                        if ([self respondsToSelector:setMethod])
                        {
                            [self performSelector:setMethod withObject:value];
                            
                        }
                        
                    }
                    
                }
                
            }
        }
        
        free(properties);
        
        superClass = [superClass superclass];
        
    }

}





/// 拼接当前属性名字的set:方法
/// @param attName 属性名称
- (SEL)getSetMethodWithAttName:(NSString *)attName
{
    // title ->setTitle:
    // 1.获取属性名字首字母的大小
    NSString *firstName = [attName substringToIndex:1].capitalizedString;
    // 2.获取剩余的名字
    NSString *lastName = [attName substringFromIndex:1];
    // 3.拼接完整的方法名字
    NSString *setAttName = [NSString stringWithFormat:@"set%@%@:",firstName,lastName];
    return NSSelectorFromString(setAttName);
}



/// 拼接当前属性名字的get:方法
/// @param attName 属性名称
- (SEL)getMethodWithAttName:(NSString *)attName
{
    return NSSelectorFromString(attName);
}




@end
